//==============================================================================
#include "Surface.h"
#include "SDL_ttf.h"
#include "Entity.h"
#include "Client.h"

//==============================================================================

TTF_Font* CSurface::font ;

CSurface::CSurface()
{
	TTF_Init();
	font = TTF_OpenFont("./images/Fonts/arial.ttf", 10);
}

 

//==============================================================================
SDL_Surface* CSurface::OnLoad(char* File) {
	SDL_Surface* Surf_Temp = NULL;
	SDL_Surface* Surf_Return = NULL;

	if((Surf_Temp = IMG_Load(File)) == NULL) {
		return NULL;
	}

	Surf_Return = SDL_DisplayFormatAlpha(Surf_Temp);
	SDL_FreeSurface(Surf_Temp);

	return Surf_Return;
}

//==============================================================================
bool CSurface::OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y) {
	if(Surf_Dest == NULL || Surf_Src == NULL) {
		return false;
	}

	SDL_Rect DestR;

	DestR.x = X;
	DestR.y = Y;

	SDL_BlitSurface(Surf_Src, NULL, Surf_Dest, &DestR);

	return true;
}

//------------------------------------------------------------------------------
bool CSurface::OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, int X2, int Y2, int W, int H,int playerID) {
	if(Surf_Dest == NULL || Surf_Src == NULL) {
		return false;
	}

	if(playerID == 1000) //Checks if the playerID == 1000, this means that the item to be drawn is not a player, and should not be drawn name and healthbar on.
	{

	}
	else
	{	
		SDL_Surface* Text_Solid;
		SDL_Rect TextPos;
		SDL_Rect Healthbar;
		TextPos.x = X-6;
		TextPos.y = Y-15;
		SDL_Colour black = {0,0,0};
		
			std::string name = CEntity::PlayerList.at(playerID)->name;
			int size = CEntity::PlayerList.at(playerID)->name.size();

			//Adjusting the name position after how long the name is.
			if(size == 1)
			{
				TextPos.x += 12;
			}
			if(size == 2)
			{
				TextPos.x += 10;
			}
			if(size == 3)
			{
				TextPos.x += 8;
			}
			if(size == 4)
			{
				TextPos.x += 6;
			}
			if(size == 5)
			{
				TextPos.x += 4;	
			}
			if(size == 6)
			{
				TextPos.x += 2;	
			}
			if(size == 7)
			{
				TextPos.x += 1;	
			}
		if(playerID == CClient::myID)//Checks that the player is your player, and sets the color to black.
		{
			SDL_Colour black = {0,0,0};
			Text_Solid = TTF_RenderText_Blended(CSurface::font, CEntity::PlayerList.at(playerID)->name.c_str(), black);
		}
		else if(playerID != CClient::myID && CEntity::PlayerList.at(playerID)->health >=10) //Checks that the player is not your player and has more than 10 health, then sets the color to black.
		{
			SDL_Colour black = {0,0,0};
			Text_Solid = TTF_RenderText_Blended(CSurface::font, CEntity::PlayerList.at(playerID)->name.c_str(), black);
		}
		else if(playerID != CClient::myID && CEntity::PlayerList.at(playerID)->health <10) //Checks that the player is not your player and has less than 10 health, then sets the color to red.
		{
			SDL_Colour black = {255,0,0};
			Text_Solid = TTF_RenderText_Blended(CSurface::font, CEntity::PlayerList.at(playerID)->name.c_str(), black);
		}
			//Sets the healthbar size and position.
			Healthbar.x = (X) + (100-CEntity::PlayerList.at(playerID)->health)/(10);
			Healthbar.y = Y-18;
			Healthbar.w = CEntity::PlayerList.at(playerID)->health/(10)*2;
			Healthbar.h = 5;
			if(playerID == CClient::myID)
			{
				Uint32 color;
				color = SDL_MapRGB(Surf_Dest->format, 127, 255, 0);
				SDL_FillRect(Surf_Dest, &Healthbar, color);	
			}
			else
			{
				SDL_FillRect(Surf_Dest, &Healthbar, 255255255255555);
			}
				
		SDL_BlitSurface(Text_Solid, NULL, Surf_Dest, &TextPos); // Blits the surface onto the screen.
		SDL_FreeSurface(Text_Solid);
		
		
	}
	

		SDL_Rect DestR;

		DestR.x = X;
		DestR.y = Y;

		SDL_Rect SrcR;

		SrcR.x = X2;
		SrcR.y = Y2;
		SrcR.w = W;
		SrcR.h = H;

		SDL_BlitSurface(Surf_Src, &SrcR, Surf_Dest, &DestR); // Blits the surface onto the screen.

		return true;
}

//------------------------------------------------------------------------------
bool CSurface::Transparent(SDL_Surface* Surf_Dest, int R, int G, int B) {
	if(Surf_Dest == NULL) {
		return false;
	}

	SDL_SetColorKey(Surf_Dest, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(Surf_Dest->format, R, G, B));

	return true;
}

//==============================================================================
